﻿component{
	
	include "../../inc/lib.cfm"; 
	public any function getTree(){
		var result = {};
		var hql = "from organization where organizationid is null order by organizationid asc,seq asc";
		var result = ormExecuteQuery(hql,false);
		return result;
	}
	
	public any function list(rc){
		var result = {};
		var hql = " from organizationflat order by organizationid asc,seq asc";
		var result["rows"] = ormExecuteQuery(hql,false);
		var totalhql = "select count(id) as total from organizationflat";
		var result["total"] = ormExecuteQuery(totalhql,true);
		
		newResult = [];
		
		for(var i=1;i<=arraylen(result.rows);i++){
			var tmpResult = {};
			tmpResult["address"] = result.rows[i].getAddress();
			tmpResult["text"] = result.rows[i].getText();
			tmpResult["id"] = result.rows[i].getId();
			tmpResult["code"] = result.rows[i].getCode();
			tmpResult["seq"] = result.rows[i].getSeq();
			tmpResult["_parentId"] = result.rows[i].getOrganizationid();
			newResult[i] = tmpResult;
		}
		result["rows"] = newResult;
		return result;
	}
	
	
	
	//获取操作
	public any function get(numeric id){
		result = entityLoadByPk("organizationflat", arguments.id);
		return result;
	}

	//新增操作，调用的是organizationflat实体对象	
	public any function save(any rc){
		transaction {
            var q = entityNew("organizationflat");
			include "../../inc/saveandupdate.cfm";
        }
        return q;
	}
	
	public any function getOrganizationResource(numeric id){
		var result = {};
		var hql = "from organizationresource where organizationid = " & id;
		var result = ormExecuteQuery(hql,false);
		return result;
	}
	
	
	//删除操作
	//删除不能用organizationflat模式，因为删除含有子类的节点时，需要利用递归将所有的子节点都进行删除
	public any function delete(numeric id){
        transaction {
            var organization = entityLoadByPk("organization", arguments.id);
            //设置全局ids的变量，ids不可变，因为递归函数中用ids
            ids ="";
            //将自身的id追加到idList这个列表中去
            idList = listprepend(RecursiveChild(organization),arguments.id); 
            
            for(var i=1;i<=listlen(idList);i++){
				//删除organization
            	var resource = entityLoadByPk("organization", listgetat(idList,i));
            	entityDelete(organization);
            }
            transactionCommit();
        }
	}

	
	//修改操作
	public any function update(any rc){
		transaction {
            var q = entityLoadByPk("organizationflat", rc.id);
			include "../../inc/saveandupdate.cfm";
        }
        return q;
	}
	
	
	//授权操作
	public any function savegrant(any rc){
		transaction {
			//在授权之前，先要检查ids是否已经存在在organizationresource中
			/*
			ids需要分成三部分
			1.需要新添加的
			2.不需要操作的
			3.要删除操作的
			
			示例如下：
			ids1 = "1,2,3,4,5"; //新传递进来的值
			ids2 = "1,3,8,9";	//原来已经存在的值
			writeoutput(":" & listCompare(ids1,ids2));
			writeoutput("不需要操作的:" & ListInCommon(ids1,ids2));
			writeoutput("要删除操作的:" & listdelete(ListInCommon(ids1,ids2),ids2));
			需要新添加的:2,4,5
			不需要操作的:1,3
			要删除操作的:8,9
			*/
			var orObj = getOrganizationResource(rc.id);
			oldIds = "";
			checkIds = "";
			
			for(var i=1;i<=arraylen(orObj);i++){
				oldIds = listAppend(oldIds,orObj[i].getResourceid().getId());
				checkIds = listAppend(checkIds,orObj[i].getId());
			}
			needInsertIds = listCompare(rc.ids,oldIds);
			keepStatusIds = ListInCommon(rc.ids,oldIds);
			needDeleteIds = listdelete(ListInCommon(rc.ids,oldIds),oldIds);
			
			//writeoutput("1:" & needInsertIds & "--2:" & keepStatusIds & "--3:" & needDeleteIds);
			//需要删除的部分
            if(listlen(needDeleteIds)){
				for(var i=1;i<=listlen(needDeleteIds);i++){
					var deleteId =  listgetat(checkIds, listfindnocase( oldIds,listgetat(needDeleteIds,i) )) ;
					var orEntity = entityLoadByPk("organizationresource", deleteId);
					entityDelete(orEntity);
				}
			}
		
			//需要添加的部分
			if(listlen(needInsertIds)){
				for(var i=1;i<=listlen(needInsertIds);i++){
					var q = entityNew("organizationresource");
					//建立resource实体，进行赋值
					var r = entityNew("resource");
					r.setid(listgetat(needInsertIds,i));
					//建立organization实体，进行赋值
					var o = entityNew("organization");
					o.setId(rc.id);					
					//将对象存储至organizationresource实体中去
					q.setResourceid(r);
					q.setOrganizationid(o);	
					entitySave(q);
				}
            }
            
            transactionCommit();
        }
        return true;
	}
}